// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
// 引用element ui自定义主题
import '../theme/index.css'
import store from './store'
import router from './router/route'
import common from './utils/common'
import filters from './utils/filters'
import axios from './utils/request'
import route from './utils/route'
import App from './App'
import './style/reset.less'
import './style/style.less'
import './style/theme.less'
import 'babel-polyfill'

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$http = axios
Vue.prototype.$method = common
Vue.prototype.$filter = filters
Vue.prototype.$routeNav = route

// 全局引入过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})

router.beforeEach((to, from, next) => {
    setTimeout(() => {
        if (!store.getters.user && !store.getters.hadUserInfo && !store.getters.routerChange) {
            // 请求登录信息
            store.dispatch('loginBySession').then(res => {
                if (res.redirect) return
                // 动态添加可访问路由表
                // store.dispatch('generateRouters', store).then(routers => {
                store.dispatch('generateAllRouters', store).then(routers => {
                    router.options.routes.push(...routers)
                    router.addRoutes(routers)
                    // 标记路由已动态更改
                    store.dispatch('updateRouters')
                    routeRedirect(next, store, to, true)
                })
            }).catch(() => {
                store.dispatch('updateRouters')
                routeRedirect(next, store, to, true)
            })
        } else {
            routeRedirect(next, store, to)
        }
    }, 0)

    function routeRedirect(next, store, to, replace) {
        let obj = router.options.routes.find((item) => {
            return item.path === ('/' + to.path.split('/')[1])
        })
        if (!window.routeAuth) window.routeAuth = store.getters.user && store.getters.user.authTree
        if (!window.routerFirst) window.routerFirst = route.jumpFirstRouter(window.routeAuth)
        if (obj && obj.path && to.path !== '/') {
            let hasAuth = route.getRouter(to)
            if (hasAuth || to.path.indexOf('401') > -1) {
                if (replace) {
                    next({...to, replace: true})
                } else {
                    next(...to)
                }
            } else {
                if (window.routerFirst) {
                    next({name: window.routerFirst.name, replace: true})
                } else {
                    next({path: '/401', replace: true})
                }
            }
        } else {
            if (window.routerFirst) {
                next({name: window.routerFirst.name, replace: true})
            } else {
                next({path: '/401', replace: true})
            }
            // if (store.getters.user.sideMenus) {
            //     let nextRoute = ''
            //     store.getters.user.sideMenus.filter(e => {
            //         match(e)
            //     })
            //
            //     if (nextRoute) {
            //         next({path: nextRoute, replace: true})
            //     } else {
            //         next({path: '/401', replace: true})
            //     }
            //
            //     // eslint-disable-next-line no-inner-declarations
            //     function match(list) {
            //         if (list.auth && !nextRoute) {
            //             if (list.children) {
            //                 list.children.filter(e => {
            //                     match(e)
            //                 })
            //             } else if (list.auth && !nextRoute) {
            //                 nextRoute = list.path
            //             }
            //         }
            //     }
            // }
        }
    }
})

// // 路由跳转后更新主题显示区域，刷新滚动条
router.afterEach((to, from) => {
    window.$history++
})

/* eslint-disable no-new */
window.$vm = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
})

if (!window.localStorage.getItem('ajaxApi')) window.localStorage.setItem('ajaxApi', '/_api')
window.$history = 0
